records = [89, 45, 23, 66, 3, -5] # a list records[2] = 20 print(records[2]) records = (89, 45, 23, 66, 3, -5) # a tuple print(records[2]) name = 'Williams' print(name[5])
{...}
, and indexes in a dictionary are keys. Keys can be strings and integers. But usually string values.
Obviously those keys should be all different in a dictionary so that a specific value can be accessed through its key.
Let's try the next example.key5 = 'country' records = {'name': 'John', 'id': 345678, 'age':20, 10: 'Kamloops', key5: 'Canada'} # {...}; key5 ??? records['age'] = 19 print(records['age']) print(records['address']) # ???
def readRecord(): records = {} # empty records records['name'] = input("Name: ") records['id'] = int(input("Id: ")) records['age'] = int(input("Age: ")) return records records = readRecord() print(records)
records = {'name': 'John', 'id': 345678, 'age':20} # {...} keys = list(records) print(keys) for k in keys: print(k) print(records[k]) print(k + "-> " + str(records[k]))
.keys()
.
Let's try the next example.records = {'name': 'John', 'id': 345678, 'age':20} # {...} keys = records.keys() print(keys) for k in records.keys(): print(k + "-> " + str(records[k]))
.values()
.
Let's try the next example.records = {'name': 'John', 'id': 345678, 'age':20} # {...} values = records.values() print(values) for v in records.values(): print(v)
.items()
.
Let's try the next example. The return values from items() are tuples, not lists nor dictionaries.records = {'name': 'John', 'id': 345678, 'age':20} # {...} items = records.items() print(items) for it in records.items(): print(it)
.get()
.
Let's try the next example. get(k, v)
returns the value of the key k
when the k
exists. If the key k
does not exist, v
will be returned.records = {'name': 'John', 'id': 345678, 'age':20} # {...} print(records['name'] + '\' lives in ' + records.get('country', 'Not defined')) records['country'] = input("Country: ") print(records['name'] + '\' lives in ' + records.get('country', 'Not defined'))
.setdefault()
.
Let's try the next example. setdefault(k, v)
sets the value v
of the key k
only when the k
does not exist. If the key k
does not exist, v
will be returned. Otherwise, the existing value will be returned.records = {'name': 'John', 'id': 345678, 'age':20} # {...} print(records['name'] + '\' lives in ' + records.setdefault('country', 'Canada')) print(records['name'] + '\' lives in ' + records.setdefault('country', 'US')) # ??? print(records['country']) # ??? records['country'] = 'US' print(records['country']) # ???
in
and not in
in
and not in
.
Let's try the next example.records = {'name': 'John', 'id': 345678, 'age':20} # {...} if 'name' in records: print('Name: ' + records['name']) if 'country' not in records: records['country'] = input("Country: ") print(records) if 'country' in records.keys(): print(records['country']) if 'Canada' not in records.values(): print("Not Canada")
students = {'John': [20, 'CS', 'TRU'], 'Caren': [19, 'MATH', 'TRU'], 'Snape': [58, 'Potion', 'Hogwarts']} print(students['John'])
students = [{'name':'John', 'age':20, 'major':'CS', 'school':'TRU'}, {'name':'Caren', 'age':19, 'major':'MATH', 'school':'TRU'}, {'name':'Snape', 'age':58, 'major':'Potion', 'school':'Hogwarts'}] print(students[0])
def printBoard(board): for r in range(len(board)): for c in range(len(board)): print(board[r][c], end='') # end='' gives no new line if c == 0 or c == 1: print('|', end='') print() # new line if r != 2: print('-+-+-') board = [[' ', 'O', ' '], ['X', ' ', 'X'], [' ', ' ', ' ']] # a tic-tac-toe board print(board[0]) print(board[1]) print(board[2]) print(board[0][1]) printBoard(board)
def printHorzLine(board): # n×n board for c in range(len(board[0]) * 2 - 1 + 2): if c % 2 == 0: print('+', end='') else: print('-', end='') print() def printBoard(board): printHorzLine(board) for r in range(len(board)): print('|', end='') for c in range(len(board)): print(board[r][c], end='') if c != len(board[r]) - 1: print('|', end='') print('|') printHorzLine(board) board = [['1', '2', '3'], ['4', '5', '6'], ['7', '8', ' ']] # a 3×3-Puzzle board printBoard(board)
[[' ','O',' '],['X','X',' '],[' ',' ',' ']]
|O| -+-+- X|X| -+-+- | |
[[4,8,5],[1,2,0],[3,6,7]]
+-+-+-+ |4|8|5| +-+-+-+ |1|2| | +-+-+-+ |3|6|7| +-+-+-+
(1, 2)
+-+-+-+ |4|8|5| +-+-+-+ |1|2| | +-+-+-+ |3|6|7| +-+-+-+